<template>
  <div v-if="FromData">
    <Modal
      v-model="modals"
      scrollable
      footer-hide
      closable
      :title="FromData.title"
      :z-index="1"
      width="700"
      @on-cancel="cancel"
    >
      <template>
        <div
          class="radio acea-row row-middle"
          v-if="FromData.action === '/marketing/coupon/save.html'"
        >
          <div class="name ivu-form-item-content">优惠券类型</div>
          <Radio-group v-model="type" @on-change="couponsType">
            <Radio :label="0">通用券</Radio>
            <Radio :label="1">品类券</Radio>
            <Radio :label="2">商品券</Radio>
          </Radio-group>
        </div>
      </template>
      <!-- <form-create
        :option="config"
        :rule="Array.from(FromData.rules)"
        @on-submit="onSubmit"
        class="formBox"
        ref="fc"
        handleIcon="false"
      ></form-create> -->

      <div>
        <Form ref="form" :rules="rules" :model="form" :label-width="80">
          <FormItem label="修改余额">
            <RadioGroup v-model="form.money_status">
              <Radio :label="1">增加</Radio>
              <Radio :label="2">减少</Radio>
            </RadioGroup>
          </FormItem>

          <div class="itemFlex">
            <FormItem label="余额">
              <InputNumber :min="0" v-model="form.money"></InputNumber>
            </FormItem>

            <FormItem label="类别" prop="desc_type">
              <Select v-show="form.money_status == 1" v-model="form.desc_type" style="width: 150px;">
                <Option value="system_add">系统充值</Option>
                <Option value="voucher_add">消费券充值</Option>
                <Option value="pay_product_refund">商品退款</Option>
              </Select>

              <Select v-show="form.money_status == 2" v-model="form.desc_type" style="width: 150px;">
                <Option value="system_sub">系统扣款</Option>
              </Select>
            </FormItem>
          </div>

          <FormItem label="修改积分">
            <RadioGroup v-model="form.integration_status">
              <Radio :label="1">增加</Radio>
              <Radio :label="2">减少</Radio>
            </RadioGroup>
          </FormItem>

          <FormItem label="积分">
            <InputNumber :min="0" v-model="form.integration"></InputNumber>
          </FormItem>
        </Form>
        <Button @click="onSubmit" type="primary" style="width: 100%;">提交</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import formCreate from '@form-create/iview';
import request from '@/libs/request';
import { mapState } from 'vuex';
export default {
  name: 'edit',
  components: {
    formCreate: formCreate.$form(),
  },
  computed: {
    ...mapState('userLevel', ['taskId', 'levelId']),
  },
  props: {
    FromData: {
      type: Object,
      default: null,
    },
    update: {
      type: Boolean,
      default: true,
    },
    form: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      rules: {
        desc_type: [{ required: true, message: '类别不能为空', trigger: 'change' }],
      },
      modals: false,
      type: 0,
      config: {
        global: {
          upload: {
            props: {
              onSuccess(res, file) {
                if (res.status === 200) {
                  file.url = res.data.src;
                } else {
                  this.Message.error(res.msg);
                }
              },
            },
          },
        },
      },
    };
  },

  methods: {
    couponsType() {
      this.$parent.addType(this.type);
    },

    // 提交表单 group
    onSubmit(formData) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request({
            url: this.FromData.action,
            method: this.FromData.method,
            data: this.form,
          })
            .then((res) => {
              if (this.update) this.$parent.getList();
              this.$Message.success(res.msg);
              this.modals = false;
              setTimeout(() => {
                this.$emit('submitFail');
              }, 1000);
            })
            .catch((res) => {
              this.$Message.error(res.msg);
            });
        } else {}
      });
    },
    // 关闭按钮
    cancel() {
      this.type = 0;
      // this.$emit('onCancel')
    },
  },
};
</script>

<style scoped lang="stylus">
.v-transfer-dom >>> .ivu-modal-content-drag {
  z-index: 2 !important;
}

.radio {
  margin-bottom: 14px;
}

.radio >>> .name {
  width: 125px;
  text-align: right;
  padding-right: 12px;
}

.itemFlex {
  display: flex;
}
</style>
